package cn.iocoder.yudao.module.member.eid.service;

import cn.iocoder.yudao.framework.business.basic.dao.query.CoreLambdaQueryWrapper;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.thirdparty.verify.idcard.IdCardVerifyStatusEnum;
import cn.iocoder.yudao.module.member.eid.dal.dataobject.MemberEidDO;
import cn.iocoder.yudao.module.member.eid.dal.mysql.MemberEidMapper;
import cn.iocoder.yudao.module.member.eid.vo.AppEidSaveReqVO;
import cn.iocoder.yudao.module.member.eid.vo.EidAuditReqVO;
import cn.iocoder.yudao.module.member.eid.vo.EidPageReqVO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import java.util.*;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.api.member.ErrorCodeConstants.EID_NOT_EXISTS;
import static cn.iocoder.yudao.module.api.member.ErrorCodeConstants.EID_VERIFIED_DUPLICATE;

/**
 * 实名信息 Service 实现类
 *
 * @author 山野羡民
 */
@Service
@Validated
public class MemberEidServiceImpl implements MemberEidService {

    @Resource
    private MemberEidMapper memberEidMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer saveIdcard(Long userId, AppEidSaveReqVO reqVO) {
        MemberEidDO entity = getIdcardByUserId(userId);
        if (entity != null && Objects.equals(entity.getStatus(), IdCardVerifyStatusEnum.SUCCESS.getValue())) {
            throw exception(EID_VERIFIED_DUPLICATE);
        }
        MemberEidDO updateObj = BeanUtils.toBean(reqVO, MemberEidDO.class);
        if (entity == null) {
            // 插入
            updateObj.setUserId(userId);
            updateObj.setStatus(IdCardVerifyStatusEnum.PENDING.getValue());
            memberEidMapper.insert(updateObj);
            entity = updateObj;
        } else {
            // 更新
            entity.setId(entity.getId());
            memberEidMapper.updateById(entity);
        }
        return entity.getStatus();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateAudit(EidAuditReqVO reqVO) {
        MemberEidDO memberEidDO = getIdcard(reqVO.getId());
        if (memberEidDO == null) {
            throw exception(EID_NOT_EXISTS);
        }
        if (Objects.equals(memberEidDO.getStatus(), IdCardVerifyStatusEnum.SUCCESS.getValue())) {
            throw exception(EID_VERIFIED_DUPLICATE);
        }
        memberEidDO.setStatus(reqVO.getStatus());
        memberEidMapper.updateById(memberEidDO);
    }

    @Override
    public MemberEidDO getIdcard(Long id) {
        return memberEidMapper.selectById(id);
    }

    @Override
    public MemberEidDO getIdcardByUserId(Long userId) {
        return memberEidMapper.selectOne(MemberEidDO::getUserId, userId);
    }

    @Override
    public Map<Long, Boolean> checkIdcardVerified(Set<Long> userIds) {
        List<MemberEidDO> list = memberEidMapper.selectList(new CoreLambdaQueryWrapper<MemberEidDO>()
                .inIfPresent(MemberEidDO::getUserId, userIds));
        Map<Long, Boolean> map = new HashMap<>();
        list.forEach(it -> map.put(it.getUserId(), IdCardVerifyStatusEnum.SUCCESS.getValue().equals(it.getStatus())));
        return map;
    }

    @Override
    public PageResult<MemberEidDO> getIdcardPage(EidPageReqVO pageReqVO) {
        return memberEidMapper.selectPage(pageReqVO);
    }

}
